package org.bling.star.client.connect;

import org.bling.star.common.command.systems.PingCommand;
import org.bling.star.common.command.systems.SystemCommand;
import org.bling.star.common.enums.SystemCommandType;
import org.bling.star.common.response.BaseResponse;
import io.netty.channel.Channel;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import lombok.extern.slf4j.Slf4j;

import java.util.Objects;

@Slf4j
public class ConnectWatcher extends ChannelInboundHandlerAdapter {
    @Override
    public void channelActive(ChannelHandlerContext ctx) throws Exception {
        Channel channel = ctx.channel();
        SystemCommand connectRequest = new SystemCommand();
        connectRequest.setCommandType(SystemCommandType.CONNECT_PING);
        channel.writeAndFlush(connectRequest);
        super.channelActive(ctx);
    }

    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
        if(msg instanceof PingCommand){
            PingCommand msg1 = (PingCommand) msg;
            log.info("PingCommand:{}",msg1.getMessage());
            ctx.writeAndFlush(new BaseResponse("200"));
        }
        super.channelRead(ctx, msg);
    }
}
